package com.example.imoocmusicdemo.activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.imoocmusicdemo.R;
import com.example.imoocmusicdemo.helps.DBOpenHelper;
import com.example.imoocmusicdemo.utils.User;
import com.example.imoocmusicdemo.utils.UserUtils;

import java.util.ArrayList;

public class ChangePwd extends BaseActivity {
    private DBOpenHelper mDB;
    private EditText oldPassword;
    private EditText newPassword;
    private Button reDay;
    private EditText userName;

    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_pwd);

        reDay = fd(R.id.reday);
        oldPassword = fd(R.id.oldpassword);
        newPassword = fd(R.id.newpassword);
        userName = fd(R.id.username);

        mDB = new DBOpenHelper(this);
        SharedPreferences sp = getSharedPreferences("user_mes", MODE_PRIVATE);

        editor = sp.edit();

        initView();

        reDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oldpass = oldPassword.getText().toString().trim();
                String newpass = newPassword.getText().toString().trim();
                String name = userName.getText().toString().trim();


                if (TextUtils.isEmpty(oldpass) || TextUtils.isEmpty(newpass) || TextUtils.isEmpty(name)) {
                    Toast.makeText(ChangePwd.this, "选项不能为空", Toast.LENGTH_SHORT).show();

                } else {
                    ArrayList<User> data = mDB.getAllData();
                    for (int i = 0; i < data.size(); i++) {
                        User user = data.get(i);   //可存储账号数量
                        Log.i("TAG", user.getName() + "_" + user.getPassword() + name.equals(user.getName()) + name);
                        if (name.equals(user.getName()) && oldpass.equals(user.getPassword())) {
                            mDB.update(name, newpass);
                            Toast.makeText(ChangePwd.this, "修改成功!请重新登陆", Toast.LENGTH_SHORT).show();
                            Intent intent1 = new Intent(ChangePwd.this, LoginActivity.class);
                            startActivity(intent1);
                            finish();
                            break;
                        } else if (name != user.getName() && oldpass != user.getPassword()) {
                            Toast.makeText(ChangePwd.this, "用户名或旧密码不正确", Toast.LENGTH_SHORT).show();
                        } else if (newpass.equals(oldpass)) {
                            Toast.makeText(ChangePwd.this, "新密码和旧密码不能一致", Toast.LENGTH_SHORT).show();
                        }

                    }

                }

            }
        });
    }


    private void initView() {
        initNavBar(true, "修改密码", false);
    }
}